﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Text.RegularExpressions;
using System.Net.Sockets;
using System.Net.Http;
namespace BookStore
{
    public partial class FrmRegister : Form
    {
        /*string IP = "127.0.0.1";
        const int PORT = 2000;*/
        cTcp tcpClient;
        public FrmRegister(cTcp t)
        {
            tcpClient = t;
            InitializeComponent();
        }

        private void BReset_Click(object sender, EventArgs e)
        {
            TRegisterUserName.Text = "";
            TRegisterPassWord.Text = "";
            TDouble.Text = "";
            TRegisterIDCard.Text = "";
        }

        private void BRegister_Click(object sender, EventArgs e)
        {
            //去除空格
            TRegisterUserName.Text = TRegisterUserName.Text.Trim();
            TRegisterPassWord.Text = TRegisterPassWord.Text.Trim();
            TDouble.Text = TDouble.Text.Trim();
            TRegisterIDCard.Text = TRegisterIDCard.Text.Trim();
            
            //检查两次密码是否相同
            if (TDouble.Text != TRegisterPassWord.Text)
            {
                MessageBox.Show("make sure password same", "Alert", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            
            //判断用户名与密码与身份证是否为空
            if(Log.CheckUserNameAndPassWordAndID(TRegisterUserName.Text,TRegisterPassWord.Text,TRegisterIDCard.Text) == false)
            { 
                return; 
            }
            else
            {
                DateTime dt = DateTime.Now;
                
                data d = new data("register",string.Format("{0},{1},{2},{3}",TRegisterUserName.Text,TRegisterPassWord.Text,TRegisterIDCard.Text,dt.ToString()));
                tcpClient.WriteData(d);
                
            }
            
         }
        private void TExit_Click(object sender, EventArgs e)
        {
                this.Close();
        }

        private void FrmRegister_Load(object sender, EventArgs e)
        {
            tcpClient.RegisterStatusRecieved += ReceiveRegisterResult;
        }

        private void ReceiveRegisterResult(string content)
        {
            var d = new data(content);
            string cmd = d.Command;
            string cnt = d.Content;
            if (cmd == "result")
            {

                //注册成功
                if (cnt == "REGISTER_OK")
                {
                    Invoke(new Action(() =>
                    {
                        MessageBox.Show("Success");
                        this.Close();
                    }
                                     ));
                }
                else if (cnt == "SAME") //数据库中已存在相同的用户名或者是ID
                {
                    MessageBox.Show("Already Got same Username or ID");
                }
            }
            else
            {
                MessageBox.Show("Wrong Register status code");
            }
        }
    }
}
